﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_DTO;
using HotelSoft.HotelManagement_BUS;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmTraCuu_KhachHang : Form
    {
        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        Double temp = 1;
        KhachHangDTO dtoKhaChHangCanTraCuu;
        ArrayList dsKhachHang;
        KhachHangBUS busKhachHang = new KhachHangBUS();
        public frmTraCuu_KhachHang()
        {
            InitializeComponent();
        }

        private void frmTraCuu_KhachHang_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            LayDanhSachKhachHang();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }

        private void LayDanhSachKhachHang()
        {
            busKhachHang = new KhachHangBUS();
            dsKhachHang = busKhachHang.LayDanhSachKhachHang();
            LoadListView(dsKhachHang);
            lblTongso.Text = lvKhachHang.Items.Count.ToString();
        }

        public void LoadListView(ArrayList dsKhachhang)
        {
            lvKhachHang.Items.Clear();
            ListViewItem item;
            KhachHangDTO dt;
            for (int i = 0; i < dsKhachhang.Count; i++)
            {
                dt=(KhachHangDTO)dsKhachhang[i];
                item = new ListViewItem();
                item.Text = (i+1).ToString();
                item.SubItems.Add(dt.CMND.ToString());
                item.SubItems.Add(dt.HOTENKH.ToString());
                item.SubItems.Add(DateTime.Parse(dt.NGAYSINH).ToShortDateString().ToString());
                item.SubItems.Add(dt.GIOITINH.ToString());
                item.SubItems.Add(dt.DIACHI.ToString());
                item.SubItems.Add(dt.QUOCTICH.ToString());
                lvKhachHang.Items.Add(item);
            }
            lblTongso.Text = lvKhachHang.Items.Count.ToString();
        }

        private void Btn_TraCuu_Click(object sender, EventArgs e)
        {
            dtoKhaChHangCanTraCuu = new KhachHangDTO();

            if (Txt_HoTen.Text == "")
                dtoKhaChHangCanTraCuu.HOTENKH = null;
            else
                dtoKhaChHangCanTraCuu.HOTENKH = Txt_HoTen.Text.ToString();
            if (Dt_NgaySinh.Value == null)
                dtoKhaChHangCanTraCuu.NGAYSINH = null;
            else
                dtoKhaChHangCanTraCuu.NGAYSINH = Dt_NgaySinh.Text.ToString();
            if (Txt_CMND.Text == "")
                dtoKhaChHangCanTraCuu.CMND = null;
            else
                dtoKhaChHangCanTraCuu.CMND = Txt_CMND.Text.ToString();

            if (Txt_DiaChi.Text == "")
                dtoKhaChHangCanTraCuu.DIACHI = null;
            else
                dtoKhaChHangCanTraCuu.DIACHI = Txt_DiaChi.Text.ToString();

            if (Txt_Sodt.Text == "")
                dtoKhaChHangCanTraCuu.SDTKH = null;
            else
                dtoKhaChHangCanTraCuu.SDTKH = Txt_Sodt.Text.ToString();

            if (Txt_QuocTich.Text == "")
                dtoKhaChHangCanTraCuu.QUOCTICH = null;
            else
                dtoKhaChHangCanTraCuu.QUOCTICH = Txt_QuocTich.Text.ToString();

            if (Ra_Nu.Checked == false && Ra_Nam.Checked == false)
                dtoKhaChHangCanTraCuu.GIOITINH = null;
            else
                if (Ra_Nu.Checked == true)
                    dtoKhaChHangCanTraCuu.GIOITINH = Ra_Nu.Text.ToString();
                else
                    dtoKhaChHangCanTraCuu.GIOITINH = Ra_Nam.Text.ToString();

            ArrayList dsTraCuuDuoc = busKhachHang.TraCuuKhachHang(dsKhachHang, dtoKhaChHangCanTraCuu);
            LoadListView(dsTraCuuDuoc);
            if (lvKhachHang.Items.Count == 0)
                MessageBox.Show("Không tìm thấy Khách Hàng này!");
            LamMoi();
        }

        public void LamMoi()
        {
            Txt_CMND.Text = "";
            Txt_HoTen.Text = "";
            Dt_NgaySinh.IsEmpty = true;
            Txt_QuocTich.Text = "";
            Txt_DiaChi.Text = "";
            Txt_Sodt.Text = "";
            Ra_Nam.Checked = false;
            Ra_Nu.Checked = false;
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            frmQuanly_KHACHHANG frm = new frmQuanly_KHACHHANG();
            frm.MAKHACHHANG = lvKhachHang.FocusedItem.SubItems[1].Text;
            frm.ShowDialog();
        }

        private void lvKhachHang_MouseHover(object sender, EventArgs e)
        {
            balloonTip1.DefaultBalloonWidth = 100;
            balloonTip1.SetBalloonText(lvKhachHang, "Hãy bấm chuột phải để thực hiện chức năng!");
            balloonTip1.ShowBalloon(lvKhachHang);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            LamMoi();
            LayDanhSachKhachHang();
        }

        private void btnInDS_Click(object sender, EventArgs e)
        {
            frmReport_KhachHang frm = new frmReport_KhachHang();
            frm.ShowDialog();
        }
    }
}
